package cn.nfwx.tv.util;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import cn.nfwx.tv.R;

/**
 * Created by zhouzechao on 2017/12/4.
 */

public class FileUtil {

    /**
     * 获取视频存放地址
     *
     * @param context
     * @return
     */
    public static File getOutputMediaFile(Context context) {
        try {
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                    || !Environment.isExternalStorageRemovable()) {
                File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_MOVIES), context.getString(R.string.app_name));
                if (!mediaStorageDir.exists()) {
                    if (!mediaStorageDir.mkdirs()) {
                        return getFileDirVod(context);
                    }
                }
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINESE).format(new Date());
                File file = new File(mediaStorageDir.getPath() + File.separator + "VOD_" + timeStamp + ".mp4");
                try {
                    file.createNewFile();
                    return file;
                } catch (Exception e) {
                    file.delete();
                    return getFileDirVod(context);
                }
            } else {
                return getFileDirVod(context);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取视频存放地址
     *
     * @param context
     * @return
     */
    public static File getOutputMediaFile(Context context, String name) {
        try {
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                    || !Environment.isExternalStorageRemovable()) {
                File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_MOVIES), context.getString(R.string.app_name));
                if (!mediaStorageDir.exists()) {
                    if (!mediaStorageDir.mkdirs()) {
                        return getFileDirVod(context);
                    }
                }
                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINESE).format(new Date());
                File file = new File(mediaStorageDir.getPath() + File.separator + "VOD_" + name + ".mp4");
                try {
                    file.createNewFile();
                    return file;
                } catch (Exception e) {
                    file.delete();
                    return getFileDirVod(context);
                }
            } else {
                return getFileDirVod(context);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * SD的file报错之后走的应用内存方法
     * java.io.filenotfoundexception open failed eacces (permission denied)
     *
     * @param context
     * @return
     * @throws IOException
     */
    public static File getFileDirVod(Context context) throws IOException {
        String filesDir = context.getFilesDir().getAbsolutePath();
        File fileDir = new File(filesDir, context.getString(R.string.app_name));
        if (!fileDir.exists()) {
            if (!fileDir.mkdirs()) {
                return null;
            }
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINESE).format(new Date());
        File file1 = new File(fileDir.getPath() + File.separator + "VOD_" + timeStamp + ".mp4");
        if (file1.exists()) {
            file1.delete();
            file1.createNewFile();
            return file1;
        } else {
            return file1;
        }
    }

    /**
     * 获取Glide造成的缓存大小
     *
     * @return CacheSize
     */
    public static long getFileSize(File file) {
        try {
            return getFolderSize(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取指定文件夹内所有文件大小的和
     *
     * @param file file
     * @return size
     * @throws Exception
     */
    private static long getFolderSize(File file) throws Exception {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (File aFileList : fileList) {
                if (aFileList != null) {
                    if (aFileList.isDirectory()) {
                        size = size + getFolderSize(aFileList);
                    } else {
                        size = size + aFileList.length();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }


    /**
     * 格式化单位
     *
     * @param size size
     * @return size
     */
    public static String getFormatSize(double size) {

        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return size + "B";
        }

        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);

        return result4.setScale(0, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
    }
}


